package scanner.com.scanner;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

import com.haha.extend.textview.CustomFontTextView;
import com.haha.extend.textview.FooterNavBar;

public class MainFragment extends FragmentActivity {
    private FooterNavBar footerNavBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setFooterNavBar();
        showFragment(ScanFragment.getInstance());
    }

    private void setFooterNavBar(){
        footerNavBar = (FooterNavBar)findViewById(R.id.footer_nav_bar);
        footerNavBar.setOnNavItemChangedListener(onNavItemChangedListener);
    }

    public void showFragment(Fragment fragment){
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content,fragment);
        fragmentTransaction.commit();
    }
    private FooterNavBar.OnNavItemChangedListener onNavItemChangedListener  = new FooterNavBar.OnNavItemChangedListener() {
        private static final int FIRST_TAB = 0;
        private static final int SECOND_TAB = 1;
        private static final int THIRD_TAB = 2;
        private static final int FOURTH_TAB = 3;

        private static final float UNCHECKED_ALPHA = 0.5f;
        private static final float CHECKED_ALPHA = 1.0f;

        @Override
        public void onNavItemChanged(CustomFontTextView[] items, CustomFontTextView item, int position) {
            for(int i = 0; i < 4; i++) items[i].setAlpha(UNCHECKED_ALPHA);
            switch (position){
                case FIRST_TAB:
                    showFragment(ScanFragment.getInstance());
                    item.setAlpha(CHECKED_ALPHA);
                    break;
                case SECOND_TAB:
                    showFragment(HistoryFragment.getInstance());
                    item.setAlpha(CHECKED_ALPHA);
                    break;

                case THIRD_TAB:
                    showFragment(MakeFragment.getInstance());
                    item.setAlpha(CHECKED_ALPHA);
                    break;

                case FOURTH_TAB:
                    showFragment(SettingsFragment.getInstance());
                    item.setAlpha(CHECKED_ALPHA);
                    break;
            }
        }
    };
}
